package com.gc.web.controller.api;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.UserSign;
import com.gc.business.service.IUserSignService;
import com.gc.common.constant.Constants;
import com.gc.common.utils.DateUtils;
import com.gc.common.utils.StringUtils;
import com.gc.framework.web.base.ApiBaseController;
import com.gc.system.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title 人员签到接口api层
 * @Date 2020/11/25 09:32
 * @author hruiyan
 */
@CrossOrigin
@Controller
@RequestMapping("/api/userSign")
public class ApiUserSignController extends ApiBaseController {


    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IUserSignService userSignService;


    /**
     * 查询个人签到记录
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/userMineSignList")
    @ResponseBody
    public Map<String,Object> userMineSignList(String createTime ,String token,Integer pageNum,Integer pageSize){

        Long userId = getUserId(token);
        UserSign sign = new UserSign();
        Date parse = DateUtil.parse(createTime, "yyyy-MM-dd");
        sign.setCreateTime(parse);
        sign.setUserId(userId);
        startPage(pageNum,pageSize,"create_time DESC");
        List<UserSign> list =
                userSignService.list(sign);

        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("list",list);
        map.put("total",new PageInfo<>(list).getTotal());
        return success(map);
    }


    /**
     * 人员签到保存记录接口
     * @param userSign
     * @param token
     * @return
     */
    @PostMapping("/userSignClockSave")
    @ResponseBody
    public Map<String,Object> userSignClockSave(UserSign userSign,String token){

        if(StringUtils.isEmpty(userSign.getLatitude()) || StringUtils.isEmpty(userSign.getLongitude())){
            return error(Constants.UNVERIFY,"缺少经纬度");
        }

        Long userId = getUserId(token);
        userSign.setUserId(userId);
        userSign.setCreateTime(DateUtil.date());
        return success(userSignService.save(userSign));
    }




}
